<template>
  <el-container>
    <el-header class="no-flex" height="auto">
      <el-form  label-width="70px" :model="searchForm" inline>
        <el-form-item label="岗位名称" class="inline" >
          <el-input v-model="searchForm.postName" placeholder="请输入岗位名称" :clearable="true" style="width: 200px"></el-input>
        </el-form-item>
        <el-form-item label="岗位编码" class="inline">
          <el-input v-model="searchForm.postCode" placeholder="请输入岗位编码" :clearable="true" style="width: 200px"></el-input>
        </el-form-item>
        <el-form-item label="启用状态" class="inline">
          <el-select v-model="searchForm.enabled" placeholder="请选择启用状态" :clearable="true">
            <el-option v-for="item in dicts.system_global_status" :key="item.v" :label="item.k" :value="item.v"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search"  @click="upsearch">
            <span>搜索</span>
          </el-button>
          <el-button type="default" icon="el-icon-RefreshLeft" @click="reset">
            <span>重置</span>
          </el-button>
        </el-form-item>
        <el-form-item style="display: flex; margin-bottom: 0">
          <el-button type="primary" icon="el-icon-plus" @click="add" v-auths="['*:*:*','system:post:add']">新增</el-button>
        </el-form-item>
      </el-form>
    </el-header>
    <el-main class="nopadding">
      <el-table   ref="table" row-key="id" @selection-change="selectionChange" :data="tableData" width="100%">
        <el-table-column type="selection" width="50" />
        <el-table-column label="岗位名称" prop="postName" width="200" />
        <el-table-column label="岗位编码" prop="postCode" width="200" />
        <el-table-column label="岗位排序" prop="sort" width="200" />
        <el-table-column label="启用状态" prop="enabled" width="200" >
          <template #default="{row}">
            <template v-for="item in dicts.system_global_status">
              <el-tag v-if="row.enabled == item.v">{{item.k}}</el-tag>
            </template>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" prop="createTime" :formatter="rendererDateTime" min-width="300"></el-table-column>
        <el-table-column label="操作" fixed="right" align="center" width="300">
          <template #default="scope">
            <el-button-group>
              <el-button  text type="primary"  @click="table_show(scope.row.id, scope.$index)" icon="el-icon-View" >查看</el-button>
              <el-button text type="primary"  v-if="scope.row.system!=1" @click="table_edit(scope.row.id, scope.$index)" icon="el-icon-Edit"
                         v-auths="['*:*:*','system:post:update']"
              >编辑</el-button>
              <el-popconfirm title="确定删除吗？" v-if="scope.row.system!=1" @confirm="table_del(scope.row, scope.$index)">
                <template #reference>
                  <el-button text type="danger" icon="el-icon-delete" v-auths="['*:*:*','system:post:delete']">删除</el-button>
                </template>
              </el-popconfirm>
            </el-button-group>
          </template>
        </el-table-column>
      </el-table>
    </el-main>
    <el-footer style="display: flex;justify-content: flex-end">
      <el-pagination
        :current-page="searchForm.current"
        :page-size="searchForm.size"
        :page-sizes="[10, 20, 50, 100]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-footer>
  </el-container>
  <save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSaveSuccess" @closed="dialog.save=false"></save-dialog>

</template>

<script>
import saveDialog from './components/save.vue'
export default {
  name: 'post',
  dicts: ['system_global_status'],
  components: {
    saveDialog
  },
  data() {
    return {
      dialog: {
        save: false
      },
      searchForm:{
        current:1,
        size:10,
        postName:'',
        postCode:'',
        enabled: ''
      },
      total:0,
      selection: [],
      tableData:[],
    }
  },
  created() {
    this.getData()
  },
  methods: {
    // 请求数据
    getData(){
      this.$API.system.post.list.get(this.searchForm).then(res=>{
        this.tableData  = res.data.records;
        this.total =res.data.total
      })
    },
    //切换分页
    handleCurrentChange(e){
      this.searchForm.current = e;
      this.getData()
    },
    // 改变单页显示数量
    handleSizeChange(e){
      this.searchForm.size = e
      this.pagi.pageSize =e
      this.getData()
    },
    // 转换时间
    rendererDateTime(row, column, cellValue, index){
      return this.$TOOL.dateFormat(cellValue)
    },
    //添加
    add(){
      this.dialog.save = true
      this.$nextTick(() => {
        this.$refs.saveDialog.open().setData()
      })
    },
    //编辑
    table_edit(id){
      this.dialog.save = true
      this.$nextTick(() => {
        this.$refs.saveDialog.open('edit').setData(id)
      })
    },
    //查看
    table_show(id){
      this.dialog.save = true
      this.$nextTick(() => {
        this.$refs.saveDialog.open('show').setData(id)
      })
    },
    //删除
    async table_del(row){
      let res = await this.$API.system.post.delPost.get(row.id)
      if(res.code == 200){
        this.$message.success('删除成功')
        this.tableData = []
        this.getData()
      } else{
        this.$message.error('删除失败')
      }
    },
    //表格选择后回调事件
    selectionChange(selection){
      this.selection = selection;
    },
    //搜索
    async upsearch(){
      let data= this.searchForm
      let res = await this.$API.system.post.list.get(data)
      this.tableData = res.data.records;
    },
    //重置功能
    reset(){
      this.searchForm={
        current:1,
        size:100,
        postName:'',
        postCode:'',
        enabled: ''
      }
      this.tableData=[];
      this.getData()
    },
    //本地更新数据
    handleSaveSuccess(){
      this.tableData=[]
      this.getData()
    }
  }
}
</script>

<style scoped>

</style>
